package com.jic.manager.controller.manage.conf;

import com.alibaba.fastjson.JSONObject;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.manager.enums.ResponseCode;
import com.jic.manager.exception.AppRuntimeException;
import com.jic.manager.entity.CmsTemplateVariable;
import com.jic.manager.entity.vo.request.CmsTemplateVariableRequest;
import com.jic.manager.entity.vo.response.CmsTemplateVariableResponse;
import com.jic.manager.service.CmsTemplateVariableService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

;

/**
 * @Author: Taozhouchuan 
 * @date: 2020-03-22 01:48:04
 * NFZD TODO 本页面需要修改package的路径；
 */
 @RestController
 @Slf4j
 @RequestMapping("/cmsTemplateVariable")
 @Api(tags = "控制类-PC")
public class CmsTemplateVariableController {
	@Autowired
	private CmsTemplateVariableService service;
	
 /**
	 * CMS_TEMPLATE_VARIABLE 查询列表（分页）
	 * @param request 前端请求参数
  */
 @PostMapping("/listPage")
 @ApiOperation(value = "分页查询cmsTemplateVariable列表")
 @ResponseBody
 public RestResult<PageResult<CmsTemplateVariableResponse>>  listCmsTemplateVariableRecordPagination(@RequestBody CmsTemplateVariableRequest request) throws Throwable {
		//分页处理
		PageResult<CmsTemplateVariableResponse> pageResult = new PageResult<>();
 		CmsTemplateVariable cmsTemplateVariable = new CmsTemplateVariable();
		try {
			BeanUtils.copyProperties(request, cmsTemplateVariable);
			List<CmsTemplateVariableResponse> list = service.listCmsTemplateVariableRecordPagination(request);
			long count = service.listPageCount(cmsTemplateVariable);
			List<CmsTemplateVariableResponse> responseList = list.stream().map(temp -> {
			CmsTemplateVariableResponse bo = new CmsTemplateVariableResponse();
			BeanUtils.copyProperties(temp, bo);
			return bo;
			}).filter(item -> item != null).collect(Collectors.toList());
		pageResult.setRows(responseList);
		pageResult.setTotal(count);
		} catch (Exception e) {
			log.error("查询异常",e);
			throw new AppRuntimeException(ResponseCode.EXCEPTION);
		}
		return RestResult.success(pageResult);
	}

/**
	 * CMS_TEMPLATE_VARIABLE 查询列表（不分页）
	 * @param  request 前端请求参数
  */
	@PostMapping("/listAll")
 @ApiOperation(value = "不分页查询cmsTemplateVariable列表")
 @ResponseBody
 public RestResult<List<CmsTemplateVariableResponse>>  listCmsTemplateVariableAllRecord(@RequestBody CmsTemplateVariableRequest request) throws Throwable {
		//分页处理
		CmsTemplateVariableRequest cmsTemplateVariableRequest = new CmsTemplateVariableRequest();
 		CmsTemplateVariable cmsTemplateVariable = new CmsTemplateVariable();
		BeanUtils.copyProperties(request, cmsTemplateVariable);
		List<CmsTemplateVariable> list = service.listCmsTemplateVariableAllRecord(cmsTemplateVariable);
			List<CmsTemplateVariableResponse> responseList = list.stream().map(temp -> {
			CmsTemplateVariableResponse bo = new CmsTemplateVariableResponse();
			BeanUtils.copyProperties(temp, bo);
			return bo;
			}).filter(item -> item != null).collect(Collectors.toList());
		return RestResult.success(responseList);
}

/**
 * 单表查询详情信息
 * @param request 查询详情请求参数
 * @return CmsTemplateVariableResponse 查询详情响应参数
 * author Taozhouchuan 
 * @date 2020-3-11 20:54:04
 */
 @PostMapping("/viewCmsTemplateVariable")
 @ApiOperation(value = "查询cmsTemplateVariable详情")
 @ResponseBody
public RestResult<CmsTemplateVariableResponse> getCmsTemplateVariableDetail(@Validated  @RequestBody CmsTemplateVariableRequest request) {
	log.info("查询详情请求参数入参，{}", JSONObject.toJSONString(request));
	if (request!=null) {
	 
		CmsTemplateVariable cmsTemplateVariable = new CmsTemplateVariable();
		BeanUtils.copyProperties(request, cmsTemplateVariable);
		CmsTemplateVariable result = service.getCmsTemplateVariable(cmsTemplateVariable);
		log.info("查询详情请求参数出参，{}", JSONObject.toJSONString(result));
		if (result == null) {
			return RestResult.error("9999", "记录不存在");
		}
		CmsTemplateVariableResponse bo = new CmsTemplateVariableResponse();
		BeanUtils.copyProperties(result, bo);
		return RestResult.success(bo);
	}
	log.info("查询详情请求结束");
	return RestResult.success(new CmsTemplateVariableResponse());
}
/**
 * 新增或修改记录
 * @param request 新增或修改请求参数
 * @return CmsTemplateVariableResponse 新增或修改响应参数
 * author Taozhouchuan
 * @date 2020-3-11 20:54:04
 */
@ApiOperation(value = "保存或更新cmsTemplateVariable")
@PostMapping("/saveOrUpdateCmsTemplateVariable")
 @ResponseBody
public RestResult<Long> saveOrUpdateCmsTemplateVariable(@Validated @RequestBody CmsTemplateVariableRequest request) throws Throwable {
		//分页处理
 		CmsTemplateVariable cmsTemplateVariable = new CmsTemplateVariable();
		BeanUtils.copyProperties(request, cmsTemplateVariable);
	long flag =   service.saveOrUpdateCmsTemplateVariable(cmsTemplateVariable);
  if(flag>0){
     return RestResult.success(flag);
	  }else{
     return RestResult.error("9999", "新增或修改异常");
   }
}
/**
 * 修改记录
 * @param request 修改请求参数
 * @return CmsTemplateVariableResponse 修改响应参数
 * author Taozhouchuan
 * @date 2020-3-11 20:54:04
 */
@ApiOperation(value = "修改cmsTemplateVariable记录")
@PostMapping("/updateCmsTemplateVariable")
@ResponseBody
public RestResult<Long> updateCmsTemplateVariable(@RequestBody CmsTemplateVariableRequest request) throws Throwable {
		//分页处理
 		CmsTemplateVariable cmsTemplateVariable = new CmsTemplateVariable();
		BeanUtils.copyProperties(request, cmsTemplateVariable);
	long flag = service.updateCmsTemplateVariable(cmsTemplateVariable);
  if(flag>0){
     return RestResult.success(flag);
	  }else{
     return RestResult.error("9999", "修改异常");
   }
}
// 注释
/**
 * 新增记录
 * @param request 新增请求参数
 * @return CmsTemplateVariableResponse 新增响应参数
 * author Taozhouchuan
 * @date 2020-3-11 20:54:04
 */
@ApiOperation(value = "新增cmsTemplateVariable记录")
@PostMapping("/addCmsTemplateVariable")
@ResponseBody
public RestResult<Long> addCmsTemplateVariable(@RequestBody CmsTemplateVariableRequest request) throws Throwable {
		//分页处理
 		CmsTemplateVariable cmsTemplateVariable = new CmsTemplateVariable();
		BeanUtils.copyProperties(request, cmsTemplateVariable);
	long flag = service.addCmsTemplateVariable(cmsTemplateVariable);
  if(flag>0){
     return RestResult.success(flag);
	  }else{
     return RestResult.error("9999", "新增异常");
   }
}
/**
 * 删除记录
 * @param request 删除请求参数
 * @return CmsTemplateVariableResponse 删除响应参数
 * author Taozhouchuan
 * @date 2020-3-11 20:54:04
 */
@ApiOperation(value = "删除cmsTemplateVariable记录")
@PostMapping("/deleteCmsTemplateVariable")
@ResponseBody
public RestResult<Long> deleteCmsTemplateVariable(@RequestBody CmsTemplateVariableRequest request) throws Throwable {
		//分页处理
 		CmsTemplateVariable cmsTemplateVariable = new CmsTemplateVariable();
		BeanUtils.copyProperties(request, cmsTemplateVariable);
	long flag = service.deleteCmsTemplateVariable(cmsTemplateVariable);
  if(flag>0){
     return RestResult.success(flag);
	  }else{
     return RestResult.error("9999", "删除异常");
   }
}
/**
 * 逻辑删除记录
 * @param request 逻辑删除请求参数
 * @return CmsTemplateVariableResponse 逻辑删除响应参数
 * author Taozhouchuan
 * @date 2020-3-11 20:54:04
 */
@ApiOperation(value = "逻辑删除cmsTemplateVariable记录")
@PostMapping("/deleteCmsTemplateVariableLogically")
@ResponseBody
public RestResult<Long> deleteCmsTemplateVariableLogically(@RequestBody CmsTemplateVariableRequest request) throws Throwable {
		//分页处理
 		CmsTemplateVariable cmsTemplateVariable = new CmsTemplateVariable();
		BeanUtils.copyProperties(request, cmsTemplateVariable);
	long flag =  service.deleteCmsTemplateVariableLogically(cmsTemplateVariable);
  if(flag>0){
     return RestResult.success(flag);
	  }else{
     return RestResult.error("9999", "逻辑删除异常");
   }
}
}
